package com.fs.service;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.mapper.GoodsFavouriteMapper;
import com.fs.mapper.GoodsImagesMapper;
import com.fs.mapper.GoodsInfoMapper;
import com.fs.mapper.GoodsMapper;
import com.fs.pojo.Goods;
import com.fs.pojo.GoodsFavourite;
import com.fs.pojo.GoodsImages;
import com.fs.pojo.GoodsInfo;
import com.fs.vo.PageResult;
import com.fs.vo.SysResult;
import com.fs.vo.GoodsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class GoodsSerivceImpl implements GoodsSerivce {
    @Autowired
    private GoodsMapper goodsDao;

    @Autowired
    private GoodsInfoMapper goodsInfoDao;

    @Autowired
    private GoodsImagesMapper goodsImagesDao;

    @Autowired
    private GoodsFavouriteMapper favouriteDao;

    @Override
    @Transactional
    public SysResult add(GoodsVO entity) {
        Date now = new Date();
        Goods goods = entity.getGoods();
        //检测标题

        //检测pid

        //检测价格等

        //第一张图片存主表
        String imgs=goods.getImages();
        if(imgs==null || "".equals(imgs)){
            return SysResult.fail("请上传图片");
        }
        String[] imgArr = imgs.split(",");
        goods.setImages(imgArr[0]);


        goods
                .setCreated(now)
                .setUpdated(now);
        goodsDao.insert(goods);

        Integer goodsId = goods.getId();

        //其他图片存副表
        if(imgArr.length>1){
            for (int i = 1; i < imgArr.length; i++) {
                goodsImagesDao.insert(new GoodsImages()
                        .setGoodsId(goodsId)
                        .setUrl(imgArr[i])
                        .setStatus(true)
                        .setCreated(now)
                        .setUpdated(now)
                );
            }
        }

        GoodsInfo goodsInfo = entity.getGoodsInfo();
        goodsInfo.setGoodsId(goodsId)
                .setCreated(now)
                .setUpdated(now);
        goodsInfoDao.insert(goodsInfo);
        return SysResult.success();
    }

//    @Override
//    @Transactional
//    public ResultVO edit(GoodsEntity entity) {
//        goodsDao.updateById(entity);
//        return ResultVO.success();
//    }

    @Override
    public SysResult list(PageResult request) {
        if(request==null)return SysResult.fail();

        int pageNum = request.getPageNum();
        int pageSize = request.getPageSize();

        if(pageNum<1)pageNum=1;
        if(pageSize<1)pageNum=10;

        Page<Goods> page = new Page<>(pageNum, pageSize);


        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(request.getQuery()),"username",request.getQuery())
                .orderByDesc("id");
        page=goodsDao.selectPage(page,queryWrapper);
        request.setTotal(page.getTotal()).setRows(page.getRecords());

        return SysResult.success(request);
    }

    @Transactional
    @Override
    public SysResult updateGoodsStatus(Goods entity) {
        goodsDao.updateById(new Goods().setId(entity.getId()).setStatus(entity.getStatus()));
        return SysResult.success();
    }

    @Override
    public SysResult updateHot(Goods entity) {
        goodsDao.updateById(new Goods().setId(entity.getId()).setHot(entity.getHot()));
        return SysResult.success();
    }

    @Override
    public SysResult updateTop(Goods entity) {
        goodsDao.updateById(new Goods().setId(entity.getId()).setTop(entity.getTop()));
        return SysResult.success();
    }

    @Override
    public SysResult updateRec(Goods entity) {
        goodsDao.updateById(new Goods().setId(entity.getId()).setRecommender(entity.getRecommender()));
        return SysResult.success();
    }

    @Override
    @Transactional
    public SysResult delById(Integer id) {
        goodsInfoDao.deleteById(id);
        goodsImagesDao.delete(new QueryWrapper<GoodsImages>().in("goods_id",id));
        goodsDao.deleteById(id);
        return SysResult.success();
    }

    @Override
    public SysResult toUserList(PageResult request) {
        if(request==null)return SysResult.fail();

        int pageNum = request.getPageNum();
        int pageSize = request.getPageSize();

        if(pageNum<1)pageNum=1;
        if(pageSize<1)pageNum=10;

        Integer id = Integer.valueOf(request.getQuery().toString());

        Page<GoodsFavourite> page = new Page<>(pageNum, pageSize);

        QueryWrapper<GoodsFavourite> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("goods_id",id)
                .eq("status",true)
                .orderByDesc("id");
        page=favouriteDao.selectPage(page,queryWrapper);
        request.setTotal(page.getTotal()).setRows(page.getRecords());

        return SysResult.success(request);
    }
}
